﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 绑定子预设与主预设中指定节点的组件，目的是为了将预设与预设拆开，保证子预设有变化时不用更改主预设，且不需要在代码中显示加载
    /// </summary>
    [AddComponentMenu("Plateface.UI/Prefab Binder")]
    public class PrefabBinder : MonoBehaviour
    {
        /// <summary>
        /// 单个绑定信息
        /// </summary>
        [Serializable]
        public class BindingInfo
        {
            /// <summary>
            /// 需要加载的预设
            /// </summary>
            public GameObject Prefab;

            /// <summary>
            /// 父节点
            /// </summary>
            public Transform Parent;
        }

        /// <summary>
        /// 所有的绑定列表
        /// </summary>
        public List<BindingInfo> BindingInfoList = new List<BindingInfo>();

        private void Awake()
        {
            BindingInfo BindingInfo = null;
            for (int i = 0; i < BindingInfoList.Count; i++)
            {
                BindingInfo = BindingInfoList[i];
                GameObject child = Instantiate(BindingInfo.Prefab);
                Transform childTrans = child.transform;

                child.SetActive(true);

                childTrans.localScale = new Vector3(1, 1, 1);
                childTrans.Rotate(Vector3.zero);
                childTrans.position = Vector3.zero;

                if (BindingInfo.Parent == null)
                {
                    childTrans.SetParent(this.gameObject.transform, false);
                }
                else
                {
                    childTrans.SetParent(BindingInfo.Parent, false);
                }
            }
        }
    }
}
